package com.runa.monitor.platinterface.netservice.equipment.dtu;

import com.runa.protocol.analysis.MetaBufferAnalysis;
import com.runa.protocol.analysis.ModbusBufferRTUAnalysis;
import com.runa.protocol.dto.IInterData;
import com.runa.protocol.dto.ModbusResponseData;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.analysis.DtuHDBufferAnalysis;
import com.runa.rccprotocol.netservice.dtu.ModbusRtuResponse;
import lombok.extern.slf4j.Slf4j;

/**
 * 2024/8/29 15:44
 * zhangYB
 */
@Slf4j
public class JunPeiHdDtuBufferAnalysis extends DtuHDBufferAnalysis {


    public JunPeiHdDtuBufferAnalysis() {
        super(new ModbusBufferRTUAnalysis());
    }

    @Override
    public int fill(byte[] data, int length, String n) {
        int fill = super.fill(data, length, n);
        log.debug("君沛收到: {}", ByteUtil.byteToHexString(data, 0, length));

        IInterData gain = gain();
        if (gain instanceof ModbusResponseData) {
            ModbusRtuResponse modbusRtuResponse = new ModbusRtuResponse();
            modbusRtuResponse.setAddress(netEquNo);
            ModbusResponseData modbusResponseData = (ModbusResponseData) gain;
            modbusResponseData.setData(ByteUtil.slice(modbusResponseData.getData(), 0, modbusResponseData.getCount()));
            modbusRtuResponse.setModbusResponse(modbusResponseData);
            interData = modbusRtuResponse;
        }

        return fill;
    }


}
